﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
namespace Sale_Operation
{
    public class ProductSortOp:Component
    {
        public override bool Save(object obj)
        {
            ProductSort SM = obj as ProductSort;
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (SM.PSort_ID == 0)
                {
                    sbSql.Append("INSERT INTO ProductSort (PSort_Code,PSort_Name,PSort_PSort_ID,PSort_Type,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("PSort_Code='" + ValueHandler.GetStringValue(SM.PSort_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PSort_Name='" + ValueHandler.GetStringValue(SM.PSort_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PSort_PSort_ID='" + ValueHandler.GetStringValue(SM.PSort_PSort_ID, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PSort_Type='" + ValueHandler.GetStringValue(SM.PSort_Type, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State='" + ValueHandler.GetStringValue(SM.State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.PSort_ID = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "新增产品类别[" + ValueHandler.GetStringValue(SM.PSort_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.产品管理);

                }
                else
                {
                    sbSql.Append("UPDATE ProductSort SET ");
                    sbSql.Append("PSort_Code='" + ValueHandler.GetStringValue(SM.PSort_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PSort_Name='" + ValueHandler.GetStringValue(SM.PSort_Name, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE PSort_ID=" + SM.PSort_ID);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "修改产品类别[" + ValueHandler.GetStringValue(SM.PSort_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.产品管理);
                }
                blRes = true;

            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改产品类别出错！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();
            return blRes;
        }

        public override bool HasExists(object obj)
        {
            
            bool blRes = true;
            try
            {
                ProductSort SM = obj as ProductSort;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM ProductSort WHERE PSort_Code='" + SM.PSort_Code + "' AND PSort_ID<>" + SM.PSort_ID + " AND State=1");
                sbSql.Append(" UNION ALL ");
                sbSql.Append(" SELECT * FROM ProductSort WHERE PSort_Name='" + SM.PSort_Name + "' AND PSort_ID<>" + SM.PSort_ID + " AND State=1");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });              
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;
            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }

        public override bool Delete(object obj)
        {
           
            bool blRes = false;
            try
            {
                ProductSort SM = obj as ProductSort;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("UPDATE ProductSort SET ");
                sbSql.Append("State=0");
                sbSql.Append("WHERE PSort_ID=" + SM.PSort_ID);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除产品类别[" + ValueHandler.GetStringValue(SM.PSort_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.产品管理);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除产品类别出错！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();

            return blRes;
        }

        public override System.Data.DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            throw new NotImplementedException();
        }

        public override object GetModel(object obj)
        {
            ProductSort SM=obj as ProductSort;
            db = new DataBase();
            try
            {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("SELECT PSort_ID,PSort_Code,PSort_Name,PSort_PSort_ID,PSort_Type,State,CreateTime,CreateMan FROM ProductSort WHERE State=1  AND  PSort_ID="+SM.PSort_ID);
            SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
            if (db.Data.Tables[0].Rows.Count > 0)
            {
                DataRow dr = db.Data.Tables[0].Rows[0];
                SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                SM.PSort_Code = ValueHandler.GetStringValue(dr["PSort_Code"]);
                SM.PSort_Name = ValueHandler.GetStringValue(dr["PSort_Name"]);
                SM.PSort_PSort_ID = ValueHandler.GetIntNumberValue(dr["PSort_PSort_ID"]);
                SM.PSort_Type = ValueHandler.GetIntNumberValue(dr["PSort_Type"]);
                SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
            }
            }catch(Exception ex)
            {}
            db.Close();
            return SM;
        }
        public override System.Data.DataTable GetList(string strCondition)
        {
            StringBuilder sbSql = new StringBuilder();
            db = new DataBase();
            try
            {
                sbSql.Append(" SELECT PSort_ID,PSort_Code,PSort_Name,PSort_PSort_ID,PSort_Type,State,CreateTime,CreateMan FROM ProductSort WHERE State=1 ORDER BY PSort_PSort_ID,PSort_ID ASC ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "加载产品类别出错！["+ex.ToString()+"]", EMenuList.产品管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        /// <summary>
        /// 获取某个类别的所有子类
        /// </summary>
        /// <param name="iPSort_ID"></param>
        /// <returns></returns>
        public DataTable GetSubProductSort(int iPSort_ID)
        {
     
            db = new DataBase();
            try
            { 
                SqlHelper.FillDataset(db.Connection, "SP_GetSubSort ", db.Data, new string[] { "dtList" }, new object[] { iPSort_ID });
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "加载产品类别出错！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }

        /// <summary>
        /// 通过大类名称获取类别信息
        /// </summary>
        /// <param name="SM"></param>
        /// <returns></returns>
        public ProductSort GetModel(ProductSort obj)
        {
            ProductSort SM = obj as ProductSort;
         
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("SELECT PSort_ID,PSort_Code,PSort_Name,PSort_PSort_ID,PSort_Type,State,CreateTime,CreateMan FROM ProductSort WHERE State=1  AND  PSort_Name='" + ValueHandler.GetStringValue(SM.PSort_Name, ValueHandler.CharHandlerType.IsRepChar) + "'");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.PSort_Code = ValueHandler.GetStringValue(dr["PSort_Code"]);
                    SM.PSort_Name = ValueHandler.GetStringValue(dr["PSort_Name"]);
                    SM.PSort_PSort_ID = ValueHandler.GetIntNumberValue(dr["PSort_PSort_ID"]);
                    SM.PSort_Type = ValueHandler.GetIntNumberValue(dr["PSort_Type"]);
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.PSort_ID = ValueHandler.GetIntNumberValue(dr["PSort_ID"]);
                }
            }
            catch (Exception ex)
            { }
            db.Close();
            return SM;
        }
        /// <summary>
        /// 通过大类名称获取类别信息
        /// </summary>
        /// <param name="SM"></param>
        /// <returns></returns>
        public ProductSort GetModelById(ProductSort obj)
        {
            ProductSort SM = obj as ProductSort;

            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("SELECT PSort_ID,PSort_Code,PSort_Name,PSort_PSort_ID,PSort_Type,State,CreateTime,CreateMan FROM ProductSort WHERE State=1  AND  PSort_ID='" + ValueHandler.GetStringValue(SM.PSort_ID, ValueHandler.CharHandlerType.IsRepChar) + "'");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.PSort_Code = ValueHandler.GetStringValue(dr["PSort_Code"]);
                    SM.PSort_Name = ValueHandler.GetStringValue(dr["PSort_Name"]);
                    SM.PSort_PSort_ID = ValueHandler.GetIntNumberValue(dr["PSort_PSort_ID"]);
                    SM.PSort_Type = ValueHandler.GetIntNumberValue(dr["PSort_Type"]);
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.PSort_ID = ValueHandler.GetIntNumberValue(dr["PSort_ID"]);
                }
            }
            catch (Exception ex)
            { }
            db.Close();
            return SM;
        }
    }
}
